package com.example.shopping_mall;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.MenuItem;

import com.example.shopping_mall.fragment.ShoppingMall_Cart_Fragment;
import com.example.shopping_mall.fragment.ShoppingMall_Message_Fragment;
import com.example.shopping_mall.fragment.ShoppingMall_Mine_Fragment;
import com.example.shopping_mall.fragment.ShoppingMall_home_Fragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<Fragment> homeFragmentList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        initView();
    }

    private void initView() {
        BottomNavigationView bnv = findViewById(R.id.home_navi);

        homeFragmentList.add(new ShoppingMall_home_Fragment());
        homeFragmentList.add(new ShoppingMall_Message_Fragment());
        homeFragmentList.add(new ShoppingMall_Cart_Fragment());
        homeFragmentList.add(new ShoppingMall_Mine_Fragment());

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        for (int i = 0; i < homeFragmentList.size(); i++)
        {
            ft.add(R.id.home_fragment_container, homeFragmentList.get(i))
                    .hide(homeFragmentList.get(i));
        }

        ft.show(homeFragmentList.get(0));
        ft.commit();

        //底部导航栏监听事件
        bnv.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener()
        {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item)
            {
                showSelect(item.getItemId());
                return true;
            }
        });
    }


    private void showSelect(int itemId)
    {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        for (int i = 0; i < homeFragmentList.size(); i++)
        {
            ft.hide(homeFragmentList.get(i));
        }

        switch (itemId)
        {
            //首页
            case R.id.home_navi_home:
                ft.show(homeFragmentList.get(0));
                break;
            //视频
            case R.id.home_navi_message:
                ft.show(homeFragmentList.get(1));
                break;
            //放映厅
            case R.id.home_navi_cart:
                ft.show(homeFragmentList.get(2));
                break;
            //我的
            case R.id.home_navi_mine:
                ft.show(homeFragmentList.get(3));
                break;
        }

        ft.commit();
    }
}